package com.dqyz.busi.service.impl;

import java.util.List;
import com.dqyz.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dqyz.busi.mapper.MemberMapper;
import com.dqyz.busi.domain.Member;
import com.dqyz.busi.service.IMemberService;

/**
 * 成员管理Service业务层处理
 * 
 * @author wangguan
 * @date 2024-09-23
 */
@Service
public class MemberServiceImpl implements IMemberService 
{
    @Autowired
    private MemberMapper memberMapper;

    /**
     * 查询成员管理
     * 
     * @param id 成员管理主键
     * @return 成员管理
     */
    @Override
    public Member selectMemberById(Long id)
    {
        return memberMapper.selectMemberById(id);
    }

    /**
     * 查询成员管理列表
     * 
     * @param member 成员管理
     * @return 成员管理
     */
    @Override
    public List<Member> selectMemberList(Member member)
    {
        return memberMapper.selectMemberList(member);
    }

    /**
     * 新增成员管理
     * 
     * @param member 成员管理
     * @return 结果
     */
    @Override
    public int insertMember(Member member)
    {
        member.setCreateTime(DateUtils.getNowDate());
        return memberMapper.insertMember(member);
    }

    /**
     * 修改成员管理
     * 
     * @param member 成员管理
     * @return 结果
     */
    @Override
    public int updateMember(Member member)
    {
        member.setUpdateTime(DateUtils.getNowDate());
        return memberMapper.updateMember(member);
    }

    /**
     * 批量删除成员管理
     * 
     * @param ids 需要删除的成员管理主键
     * @return 结果
     */
    @Override
    public int deleteMemberByIds(Long[] ids)
    {
        return memberMapper.deleteMemberByIds(ids);
    }

    /**
     * 删除成员管理信息
     * 
     * @param id 成员管理主键
     * @return 结果
     */
    @Override
    public int deleteMemberById(Long id)
    {
        return memberMapper.deleteMemberById(id);
    }

    @Override
    public Member registAndLogin(String openId, String phoneNumber) {
        //根据openId查询用户信息
        Member member = new Member();
        member.setWechatOpenid(openId);
        List<Member> memberList = memberMapper.selectMemberList(member);
        if(memberList.isEmpty()) { // 如果是新用户，进行注册
            member.setWechatOpenid(openId);
            member.setPhone(phoneNumber);
            member.setNickname("新用户"+phoneNumber);
            int i = memberMapper.insertMember(member);
            System.out.println("新用户注册成功" + member.toString());
        } else { //如果是老用户，登录
            member = memberList.get(0);
        }
        return member;
    }

}
